package logic

import (
	"context"
	"fmt"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddRateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddRateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddRateLogic {
	return &AddRateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddRateLogic) AddRate(in *productrpc.AddRateRequest) (*productrpc.AddRateResponse, error) {
	// todo: add your logic here and delete this line

	var rate mysql.Rate
	rate = mysql.Rate{
		UserId:    in.UserId,
		ProductId: in.ProductId,
		Content:   in.Content,
	}
	err := mysql.DB.Debug().Create(&rate).Error
	if err != nil {
		return nil, fmt.Errorf("商品评论添加失败")
	}

	return &productrpc.AddRateResponse{Id: int64(rate.ID)}, nil
}
